dojo.provide("lorelei.articles");
// Lorelei articles view page script
// Copyright (c)2008 Logue <http://logue.be/> All rights reserved.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

lorelei.articles = {
	meta: {
		"@prefix": "<http://purl.org/net/ns/doas#>",
		"@about": "<http://mabinogi.logue.be/js/lolerei/articles.js>", a: ":JavaScript",
		 title: "Lorelei Article page script",
		 created: "2008-07-27", release: {revision: "1.6", created: "2008-10-29"},
		 author: {name: "Logue", homepage: "<http://logue.be/>"},
		 license: "<http://www.gnu.org/licenses/gpl-3.0.html>"
	},
/*
	view : function(){
		dojo.require("lorelei.mml.player");
		lorelei.mml.player.PlayButton = dijit.byId('Play');	// ボタンの定義
		lorelei.mml.player.StopButton = dijit.byId('Stop');
		lorelei.mml.player.Container = dojo.byId('media');
		dojo.query("input[name='PlayType']").connect('onclick',function(item){
			lorelei.mml.player.selector(item.target.value);
		});
		lorelei.mml.player.id = this.id;
		lorelei.mml.player.init();

		// エクスポート処理
		dojo.query('.Export').connect('onclick', function(elem){
			dojo.byId('ExportMenu').disabled = true;
			// id属性をファイルの種類に割り当てるが、メニュー化の際にidに_textがつくので削除
			window.open('../../mmls/export/'+lorelei.articles.id+'/'+elem.target.id.replace("_text", ""));
			dojo.byId('ExportMenu').disabled = false;
		});

		// 管理メニュー
		if (this.user == auth_id){
			dojo.query('.Manage').connect('onclick', function(elem){
				dojo.byId('ManageMenu').disabled = true;
				var action = elem.target.id.replace("_text", "")
				switch (action){
					case 'Edit':
						location.href = '../edit/'+lorelei.articles.id;
					break;
					case 'Delete':
						if( window.confirm(message['delete']) == true){
							location.href = '../delete/'+ lorelei.articles.id;
						}else{
							dojo.byId('ManageMenu').disabled = false;
						}
					break;
					case 'AddMml':
						location.href = '../../mmls/add/'+lorelei.articles.id;
					break;
				}
			});
		}

		// MMLのトラックごとのボタンの処理。id属性に実行するコマンドとMMLのidが入る
		dojo.query('.mml_btn').connect('onclick', function(elem){
			elem.target.disabled = true;
			var request = elem.target.id.split("_");
			id = request[1];
			switch(request[0]){
				case 'play':
					dojo.byId('media').innerHTML = '<img src="/img/loading_bar.gif" />';
					dojo.require("dojox.embed.Quicktime");
					dojox.embed.Quicktime({ path: '../../mmls/play/'+id,
						id: "player",
						width: 220,
						height: 16
					},dojo.byId('media'));
					elem.target.disabled = false;
				break;
				case 'copy':
					if( window.confirm(message['clipboard']) == true){
						dojo.xhrGet({
							url: '../../mmls/mml/'+id,
							handleAs: "text",
							timeout: 5000, // Time in milliseconds
							// The LOAD function will be called on a successful response.
							load: function(response, ioArgs) {
								dojo.require("lorelei.setClipboard");
								lorelei.setClipboard(response);
								elem.target.disabled = false;
								return response;
							},
							error: function(response, ioArgs) { 
								console.error("dojo.xhrGet: Failure");
								console.log("HTTP status code: ", ioArgs.xhr.status);
								return response;
							}
						});
					}
					elem.target.disabled = false;
				break;
				case 'edit':
					location.href = '../../mmls/edit/'+ id;
				break;
				case 'delete':
					if( window.confirm(message['delete']) == true){
						location.href = '../../mmls/delete/'+ id;
					}else{
						elem.target.disabled = false;
					}
				break;
			}
		});
		
		// タグ削除ボタン
		dojo.query('.LoreleiIconRemove').connect('onclick', function(elem){
			elem.target.disabled = true;
			if( window.confirm(message['delete']) == true){
				location.href = '../remove_tag/'+article_id + '/' + this.value;
			}else{
				elem.target.disabled = false;
			}
		});

		// 記事の翻訳
		var i = 0;
		lorelei.translate.translate('div#post p');
	},	
*/
	// 記事一覧画面の処理
	index : function(){
		// MIDI再生ボタン
		dojo.require("dojox.embed.Quicktime");
		dojo.query('.LoreleiIconPlayMidi').connect('onclick',function(elem){
			elem.target.disabled = true;
			dojo.byId('media').innerHTML = '<img src="/img/loading_bar.gif" />';
			dojox.embed.Quicktime({ path: '../../mmls/export/'+this.value+'/midi',
				id: "player",
				width: 220,
				height: 16
			},dojo.byId('media'));
			elem.target.disabled = false;
		});
		// 編集ボタン
		dojo.query(".LoreleiIconEdit").connect('onclick',function(){
			location.href='/articles/edit/'+this.value;
		});
	},
	// フォーム画面の処理
	form : function(FormName){
		// キャンセルボタン
		dojo.addOnLoad(function (){
			dojo.connect(dojo.byId(FormName), 'onsubmit', function () {
				dojo.byId('ArticleBody').value = dijit.byId('Editor').getValue(false);
				if (lorelei.mml.checker){
					lorelei.mml.checker.check_rank();
					dojo.byId('MmlRankId').value = lorelei.mml.checker.rank;
					dojo.byId('ArticleRankId').value = lorelei.mml.checker.rank;
				}
			});
			dojo.connect(dojo.byId('cancel'),'onclick',function(elem){
				this.disabled = true;
				if(article_id){
					location.href = '../../articles/view/'+article_id;
				}else{
					location.href = '../../articles/';
				}
				this.disabled = false;
			});
		});
		
		
		// ページ移動時に確認ダイアログ
		dojo.addOnUnload(function(){
			return confirm(message['move_page']); 
		});
	}
}

// メモリーリーク対策
dojo.addOnUnload(function(){
	if (dojo.byId('media')) dojo.byId('media').innerHTML = '<img src="' + dojo.moduleUrl("lorelei", "resource/loading_bar.gif") + '" />';
});

console.log(lorelei.articles.meta.title,lorelei.articles.meta.release,' has been loaded.');